package cn.cvs.servlet;


import cn.cvs.dao.MovieDao;
import cn.cvs.pojo.Movie;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/addServlet")
public class addServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        Movie movie = new Movie();
        movie.setMoviename(req.getParameter("moviename"));
        movie.setMovietype(req.getParameter("movieType"));
        movie.setPrice(req.getParameter("price"));
        movie.setRecommend(Integer.parseInt(req.getParameter("recommend")));
        movie.setStarttime(req.getParameter("startTime"));
        movie.setEndtime(req.getParameter("endTime"));
        movie.setVotes(Integer.parseInt(req.getParameter("votes")));
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int add = sqlSession.getMapper(MovieDao.class).add(movie);
        PrintWriter out = resp.getWriter();
        if(add>0){
            out.print("<script>\n" +
                    "    alert(\"添加成功\")\n" +
                    "    location.href=\"index.jsp\";\n" +
                    "</script>");
        }else {
            out.print("<script>\n" +
                    "    alert(\"添加失败\")\n" +
                    "    location.href=\"add.jsp\";\n" +
                    "</script>");
        }
    }
}
